﻿@model BabyShop.Site.Models.Product
@{
    ViewBag.Title = Model.ProductGuid == Guid.Empty ? "Create Product" : "Edit Product";
}

@using (Ajax.BeginForm("Create", null, new AjaxOptions {
    HttpMethod = "post",
    OnSuccess = "CreateSucc",
    OnFailure = "CreateFailure"
}, new { id = "CreateForm" })) {
    @Html.ValidationSummary(true)
    <fieldset class="FormFieldset">
        <legend>@ViewBag.Title</legend>
        <div class="editor-label">
            @Html.LabelFor(model => model.ProductCode)
        </div>
        <div class="editor-field">
            @Html.LabelForModel(Model.ProductCode)
            @Html.HiddenFor(m => m.ProductCode)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.ProductTypeGuid)
        </div>
        <div class="editor-field">
            @Html.DropDownListFor(m => m.ProductTypeGuid, ViewBag.ProductTypeList as IEnumerable<SelectListItem>, "请选择")
            @Html.ValidationMessageFor(model => model.ProductTypeGuid)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.ProductName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.ProductName)
            @Html.ValidationMessageFor(model => model.ProductName)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.ProductTitle)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.ProductTitle)
            @Html.ValidationMessageFor(model => model.ProductTitle)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.Price)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Price)
            @Html.ValidationMessageFor(model => model.Price)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.ProductPhoto)
        </div>
        <div class="editor-field">
            @{
                IEnumerable<SelectListItem> productPhotoList = new List<SelectListItem>();
                if(ViewBag.ProductPhotoList != null) {
                    productPhotoList = ViewBag.ProductPhotoList as IEnumerable<SelectListItem>;
                }
              }
            @Html.DropDownListFor(model => model.ProductPhoto, productPhotoList, "请上传")
            @Html.ValidationMessageFor(model => model.ProductPhoto)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.ProductIntro)
        </div>
        <div class="editor-field">
            @Html.TextAreaFor(model => model.ProductIntro)
            @Html.ValidationMessageFor(model => model.ProductIntro)
        </div>
    </fieldset>
    
    <div style="text-align: right;">
        <p>
            @Html.ActionLink("返回", "Index", null, new { id = "btnBack" })
            <input id="btnSubmit" type="submit" value="保存" />
        </p>
    </div>
    @Html.HiddenFor(m => m.ProductGuid)
}
@section HeaderScript{
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/xheditor-1.1.11-zh-cn.min.js")" type="text/javascript"></script>
    <style type="text/css">
        fieldset.FormFieldset
        {
            width:97%;
            margin:10px auto;
        }
        fieldset.FormFieldset *
        {
            margin:0;
        }
        fieldset.FormFieldset .editor-label
        {
            margin-top:8px;
            margin-bottom:8px;
        }
        .editor-label,.editor-field{display: inline-block;}
        .editor-label
        {
            width: 156px;
            text-align:right;
        }
        .editor-label:after{content: ':';}
        .editor-field
        {
            width: 80%;
        }
    </style>
    <script type="text/javascript">
        function CreateSucc(data, status) {
            if (status == 'success') {
                if (data.ErrorCode != 0)
                    alert(data.ShowMsg);
                else {
                    $.bs.showMsg('提示', data.ShowMsg);
                    $.bs.loadInContainer('@Url.Action("Index", "Product")');
                }
            }
        }

        function CreateFailure() {
            alert("Create failure.");
        }

        function Uploaded(imgItems) {
            for (var i = 0; i < imgItems.length; i++) {
                $('#ProductPhoto').append('<option value="' + imgItems[i].url.replace('!', '') + '">' + imgItems[i].localname + '</option>');
            }
        }

        $(function () {
            $('#btnSubmit,#btnBack').button();
            $('#btnBack').click(function () {
                $.bs.loadInContainer(this.href);
            });
            $('#ProductIntro').xheditor({
                     tools: 'full', 
                    skin: 'vista', 
                    width: '650', 
                    height: '250',
                    layerShadow: 2,
                    onUpload: Uploaded,
                    upImgUrl: '!{editorRoot}xheditor_plugins/multiupload/multiupload.html?uploadurl=@Url.Action("Upload","Photo")%3Fimmediate%3D1&ext=图片文件(*.jpg;*.jpeg;*.gif;*.png)',
                    upFlashUrl: '!{editorRoot}xheditor_plugins/multiupload/multiupload.html?uploadurl=@Url.Action("Upload","Photo")%3Fimmediate%3D1&ext=Flash动画(*.swf)',
                    upMediaUrl: '!{editorRoot}xheditor_plugins/multiupload/multiupload.html?uploadurl=@Url.Action("Upload","Photo")%3Fimmediate%3D1&ext=多媒体文件(*.wmv;*.avi;*.wma;*.mp3;*.mid)'
                });
        });
    </script>
}
